﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;
using UmlControlLibrary;
using System.Security;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace UmlControlLibrary
{
    public class LineDragThumb : Thumb
    {
        public static readonly RoutedEvent OnEndDragEvent = EventManager.RegisterRoutedEvent("OnEndDrag", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LineDragThumb));

        public event RoutedEventHandler OnEndDrag
        {
            add { AddHandler(LineDragThumb.OnEndDragEvent, value); }
            remove { RemoveHandler(LineDragThumb.OnEndDragEvent, value); }
        }

        private Point originalPosition;
        private UmlDiagramControl rootElement;
        private Line linkLine;

        public SelectableItemControl SelectedTarget
        {
            get;
            set;
        }

        public LineDragThumb(UmlDesignerElement parent, MouseButtonEventArgs e)
        {
            rootElement = parent.OwnerElement.OwnerElement as UmlDiagramControl;

            originalPosition = rootElement.GetMousePos(e);
            rootElement.AddChild(this);
            Canvas.SetLeft(this, originalPosition.X);
            Canvas.SetTop(this, originalPosition.Y);

            linkLine = new Line();
            linkLine.Stroke = Brushes.Black;
            linkLine.Fill = Brushes.Black;
            linkLine.X1 = originalPosition.X;
            linkLine.Y1 = originalPosition.Y;
            linkLine.X2 = linkLine.X1;
            linkLine.Y2 = linkLine.Y1;

            rootElement.AddChild(linkLine);

            base.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(LineDragThumb_MouseUp);
            base.DragDelta += new DragDeltaEventHandler(LineDragThumb_DragDelta);
            this.Width = 1;
            this.Height = 1;

            this.StartDrag(e);
        }

        public virtual void StartDrag(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            IsDragging = true;
        }

        void LineDragThumb_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //this.Visibility = Visibility.Visible;
            this.RaiseEvent(new RoutedEventArgs(OnEndDragEvent));
            rootElement.RemoveChild(this);
            rootElement.RemoveChild(this.linkLine);
        }

        public virtual void LineDragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            double deltaX = e.HorizontalChange;
            double deltaY = e.VerticalChange;
            double left = Canvas.GetLeft(this);
            double top = Canvas.GetTop(this);

            deltaX += originalPosition.X;
            deltaY += originalPosition.Y;

            left = Math.Max(left + deltaX, 0);
            top = Math.Max(top + deltaY, 0);

            Canvas.SetLeft(this, left);
            Canvas.SetTop(this, top);

            linkLine.X2 = left;
            linkLine.Y2 = top;

            SelectableItemControl control = rootElement.GetControlAtPos(new Point(left, top));
            if (control != null)
            {
                DragThumb c = (DragThumb)control.GetTemplateElement(control, "DraggableClassView");
                if (c != null)
                {
                    FrameworkTemplate template = (c as Control).Template;
                    control = (SelectableItemControl)template.FindName(template.VisualTree.FirstChild.Name, c);
                    SelectedTarget = control;
                    control.Select();
                }
            }
        }
    }

}